package com.sqfw.project.wpReport.service.impl;


import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpBackInfo;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.domain.vo.WpBackInfoVO;
import com.sqfw.project.wp.mapper.WpBackInfoMapper;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wpReport.domain.SeatEvaluateReport;
import com.sqfw.project.wpReport.domain.WpInfoEvaluate;
import com.sqfw.project.wpReport.domain.dto.BackCountDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.ISeatEvaluateReportService;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 后台坐席评价汇总表Service业务层处理
 *
 * @author lx
 * @date 2023-07-26
 */
@Slf4j
@Service
public class SeatEvaluateReportServiceImpl implements ISeatEvaluateReportService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private WpBackInfoMapper wpBackInfoMapper;

    //话务员组部门id
    private static final Long deptId = 103L;

    @Override
    public TableDataInfo getSeatEvaluateReportList(SeatEvaluateReport seatEvaluateReport) {

        List<SeatEvaluateReport> seatEvaluateReports = new ArrayList<>();
        SysUser sysUser = new SysUser();
        sysUser.setCode(seatEvaluateReport.getCode());
        sysUser.setNickName(seatEvaluateReport.getNickName());
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        //未查询到对应坐席人员,直接返回
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo(sysUsers).getTotal());
        if (sysUsers.size() == 0) {
            tableDataInfo.setRows(seatEvaluateReports);
            tableDataInfo.setMsg("查询成功");
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            return tableDataInfo;
        }
        WpInfo wpInfo = new WpInfo();
        wpInfo.setScidType("12345工单");
        wpInfo.setBegainTime(seatEvaluateReport.getBegainTime());
        wpInfo.setEndTime(seatEvaluateReport.getEndTime());
        List<String> codes = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
        wpInfo.setCodes(codes);
        List<WpInfo> wpInfosCon = wpInfoMapper.selectWpInfoList(wpInfo);


        //退单
        WpBackInfo wpBackInfo = new WpBackInfo();

        //wpBackInfo.setState("督导处理");
        wpBackInfo.setNextState("坐席处理");
        wpBackInfo.setScidType("12345工单");
        wpBackInfo.setCodes(codes);
        wpBackInfo.setBegainTime(seatEvaluateReport.getBegainTime());
        wpBackInfo.setEndTime(seatEvaluateReport.getEndTime());
        // List<BackCountDTO> dtos = wpBackInfoMapper.countWpBackInfoGroupByBackClass(wpBackInfo);

        List<WpBackInfoVO> backClassSplitCon =  wpBackInfoMapper.selectBackClassSplit(wpBackInfo);

        long l = System.currentTimeMillis();
        for (SysUser user : sysUsers) {
          //  wpInfo.setCode(user.getCode());
            //获取一段时间内坐席人员对应的所有的123456工单
            List<WpInfo> wpInfos = wpInfosCon.stream()
                    .filter(infoCon->StringUtils.isNotEmpty(infoCon.getCode()))
                    .filter(info->user.getCode().equals(info.getCode()))
                    .collect(Collectors.toList());

            SeatEvaluateReport report = new SeatEvaluateReport();

            report.setNickName(user.getNickName());
            report.setUserName(user.getUserName());
            report.setCode(user.getCode());
            int wpCount = wpInfos.size();
            report.setWpCount(wpCount);
            //总分为工单数乘以60
            int countPoint = wpCount * 60;
            report.setCountPoint(countPoint);


            List<WpBackInfoVO> collect1 = backClassSplitCon.stream()
                    .filter(infoVo-> StringUtils.isNotEmpty(infoVo.getCode())&&StringUtils.isNotEmpty(infoVo.getBackClass()))
                    .filter(wpBackInfoVO -> user.getCode().equals(wpBackInfoVO.getCode()))
                    .collect(Collectors.toList());
            List<String> backClassList = new ArrayList<>();

            for (WpBackInfoVO wpBackInfoVO : collect1) {
                String backClass = wpBackInfoVO.getBackClass();
                String[] split = backClass.split(",");

                List<String> collect = Arrays.stream(split).collect(Collectors.toList());
                backClassList.addAll(collect);
            }

         /*   for (String s : collect1) {
                String[] split = s.split(",");

                List<String> collect = Arrays.stream(split).collect(Collectors.toList());
                backClassList.addAll(collect);
            }*/

            if (backClassList.size()>0){
               List<String> checkPoint = backClassList.stream().filter("勾选错误"::equals).collect(Collectors.toList());
               report.setCheckPoint(checkPoint.size()*15);

               List<String> formatPoint = backClassList.stream().filter("格式错误"::equals).collect(Collectors.toList());
               report.setFormatPoint(formatPoint.size()*15);

               List<String> processPoint = backClassList.stream().filter("过程不明晰"::equals).collect(Collectors.toList());
               report.setProcessPoint(processPoint.size()*30);

                List<String> otherPoint = backClassList.stream().filter("其他"::equals).collect(Collectors.toList());
                //统计条数，不统计分数
                report.setOtherPoint(otherPoint.size());
           }

            if (report.getCheckPoint()==null){
                report.setCheckPoint(0);
            }
            if (report.getFormatPoint()==null){
                report.setFormatPoint(0);
            }
            if (report.getProcessPoint()==null){
                report.setProcessPoint(0);
            }
            if (report.getOtherPoint()==null){
                report.setOtherPoint(0);
            }
           /* int countDeductPoint = dtos.stream().map(BackCountDTO::getCountNumber).reduce(Integer::sum)
                    .orElse(0);*/
            int countDeductPoint = report.getCheckPoint()+report.getFormatPoint()+report.getProcessPoint();
            report.setCountDeductPoint(countDeductPoint);

            report.setTotalPoint(countPoint - countDeductPoint);
            report.setAveragePoint(60);

            seatEvaluateReports.add(report);

        }
        log.info("坐席-流转工单得分表service运行时间：{}",System.currentTimeMillis()-l);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setRows(seatEvaluateReports);

        return tableDataInfo;
    }

}
